// by Xeno
#define __DEBUG__
//#define __GROUPDEBUG__
#define THIS_FILE "fn_docreatenexttarget.sqf"
#include "x_setup.sqf"
private ["_counter", "_tmppos", "_dirn","_current_target_pos", "_current_target_radius"];
if (count _this == 2) then {
	PARAMS_2(_current_target_pos,_current_target_radius);
} else {
	PARAMS_5(_bla,_current_target_pos,_current_target_radius,_bla3,_bla4);
	GVAR(target_radius) = _bla3;
	GVAR(mttarget_radius_patrol) = _bla4;
};

__TRACE_1("_this","_this")

if (isServer && {!isNil QGVAR(HC_CLIENT_OBJ)}) exitWith {
	[QGVAR(docnt), [GVAR(HC_CLIENT_OBJ), _current_target_pos, _current_target_radius, GVAR(target_radius), GVAR(mttarget_radius_patrol)]] call FUNC(NetCallEventSTO);
};

GVAR(house_objects) = nearestObjects [_current_target_pos, ["house"], 700];

GVAR(delvecsmt) = [];
GVAR(delinfsm) = [];
GVAR(respawn_ai_groups) = [];
GVAR(mt_done) = false;


if !(__TTVer) then {	
	GVAR(enemyai_respawn_pos) = [getPosATL GVAR(FLAG_BASE), _current_target_pos] call FUNC(posbehind2); // startpoint for random camp location (if needed) plus direction
	if (surfaceIsWater (GVAR(enemyai_respawn_pos) select 0)) then {
		__TRACE("Position is in water");
		private ["_counter", "_tmppos", "_dirn" ,"_dist"];
		_counter = 0;
		_tmppos = GVAR(enemyai_respawn_pos) select 0;
		__TRACE_1("",QGVAR(enemyai_respawn_pos));
		_dist = GVAR(enemyai_respawn_pos) select 2;
		_dirn = GVAR(enemyai_respawn_pos) select 1;
		if (_dirn < 0) then {_dirn = _dirn + 180};
		_incdir = if (_dirn <= 90 || {_dirn >= 270}) then {
			-15
		} else {
			15
		};
		__TRACE_3("","_dist","_dirn","_incdir");
		_foundpos = false;
		while {_counter < 2} do {
			for "_i" from 1 to 8 do {
				_ndir = _dirn + (_incdir * _i);
				_x1 = (_current_target_pos select 0) - (_dist * sin _ndir);
				_y1 = (_current_target_pos select 1) - (_dist * cos _ndir);
				if (!surfaceIsWater [_x1, _y1]) exitWith {
					_tmppos = [_x1, _y1, 0];
					_foundpos = true;
				};
			};
			if (_foundpos) exitWith {
				__TRACE_1("","_tmppos");
			};
			__INC(_counter);
			_incdir = _incdir * -1;
		};
		
		if (surfaceIsWater _tmppos) then {
			//hint ((localize "STR_DOM_MISSIONSTRING_941") + _current_target_name + " found!!!!!");
			//diag_log ((localize "STR_DOM_MISSIONSTRING_941") + _current_target_name + " found!!!!!");
			_tmppos = _current_target_pos;
		};
		
		GVAR(enemyai_respawn_pos) set [0, _tmppos];
		_dirn = [_current_target_pos, _tmppos] call FUNC(DirTo);
		_dirn = _dirn + 180;
		// TYRGHEN - Was setting position 2 in place of 1 overwriting the distance and explaining some bugs we had with respawns.
		GVAR(enemyai_respawn_pos) set [1, _dirn];
	};

	GVAR(enemyai_mt_camp_pos) = [GVAR(enemyai_respawn_pos) select 0, 600, 400, GVAR(enemyai_respawn_pos) select 1] call FUNC(GetRanPointSquare);
	#ifdef __GROUPDEBUG__
	if (str(markerPos QUOTE(enemyai_mt_camp_pos)) != "[0,0,0]") then {deleteMarkerLocal QUOTE(enemyai_mt_camp_pos)};
	[QUOTE(enemyai_mt_camp_pos),GVAR(enemyai_mt_camp_pos),"ICON","ColorBlack",[1,1],"enemy camp pos",0,"mil_dot"] call FUNC(CreateMarkerLocal);
	#endif
	// TYRGHEN - We should create a few buildings in that place to make for a camp
	// "Land_Cargo_HQ_V1_F"
};

[_current_target_pos, _current_target_radius] spawn FUNC(createmaintarget);